/*
 * @Author: your name
 * @Date: 2021-05-19 15:22:51
 * @LastEditTime: 2021-05-25 14:54:20
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /vite-project/src/router/helper/menuHelper.ts
 */
import { AppRouteModule } from '@/router/types';
import type { MenuModule, Menu, AppRouteRecordRaw } from '@/router/types';
import { isUrl } from '@/utils/is';

function joinParentPath(menus: Menu[], parentPath = "") {

    for (let index = 0; index< menus.length; index++ ) {
        const menu = menus[index];

        if (!(menu.path.startsWith('/') || isUrl(menu.path))) {
            // path doesn't start with /, nor is it a url, join parent path
            menu.path = `${parentPath}/${menu.path}`;
        }
        if (menu?.children?.length) {
            joinParentPath(menu.children, menu.path);
        }
    }
}

export function transformMenuModule(menuModule: MenuModule): Menu {

    const { menu } = menuModule;

    const menuList = [menu];
    joinParentPath(menuList);

    return menuList[0];
}